package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void insertUser(User user) {
        user.setLastTime(new Timestamp(System.currentTimeMillis()));
        userMapper.insert(user);
    }

    public User getUserById(Integer id){
        return userMapper.getUserById(id);
    }

    @Override
    public List<User> getUserBySxOri(int index,String gender, String sexualOrien) {
        Page<User> page=new Page<>(index,9);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("gender",gender);
        queryWrapper.eq("sexualOrien",sexualOrien);
        userMapper.selectPage(page,queryWrapper);
        return page.getRecords();
    }
    @Override
    public User getUserByName(String username){
        return userMapper.getUserByName(username);
    }

    public String updateState(int id){
        Timestamp now=new Timestamp(System.currentTimeMillis());
        User user=userMapper.getUserById(id);
        //System.out.println(user);
        if(user.getState().equals("off")) return "off";
        if(user.getLastTime().after(now)){
            return "others";
        }else{
            if(user.getState().equals("lock")){
                user.setState("off");
                userMapper.update(user,null);
                return "off";
            }

        }
        return "others";
    }
}
